package ru.pb.game.network.engine.client.packets.sendable;

import ru.pb.game.network.engine.client.packets.GameServerToClientPacket;
import ru.pb.game.objects.model.players.Player;
import ru.pb.game.objects.model.players.enums.SLOT_STATE;
import ru.pb.game.objects.model.players.enums.Team;
import ru.pb.game.objects.model.room.Room;
import ru.pb.game.objects.model.room.Room.SLOT;
import ru.pb.global.data.model.ClanInfo;
import ru.pb.global.data.model.ClanLogo;
import ru.pb.global.data.model.PlayerInfo;

public class PROTOCOL_BATTLE_ENDBATTLE_ACK extends GameServerToClientPacket
{
	private Player _player;
	private PlayerInfo _playerInfo;
	private ClanInfo _clanInfo;
	private ClanLogo _clanLogo;
	private Room r;

	public PROTOCOL_BATTLE_ENDBATTLE_ACK(Player player)
	{
		_player = player;
		_playerInfo = _player.getPlayerInfos();
		// TODO: Фил, я думаю что я не совсем корректно сделал
		r = player.getRoom();
		r.changeSlotState(player.getSlot(), SLOT_STATE.SLOT_STATE_NORMAL);
		// временный фикс
		if(player.isRoomLeader())
		{
			if(r.getPlayersByTeam(Team.CHARACTER_TEAM_BLUE).size() - 1 > 0 && r.getPlayersByTeam(Team.CHARACTER_TEAM_RED).size() - 1 > 0)
			{
				r.setNewLeader(player.getObjectId());
			}
			else
			{
				r.setInFight(false);
				r.stopBattle(player.getObjectId());
			}
		}
		//
		_log.info("Player >>>>>" + _player.getName() + " exit battle");
	}

	@Override
	public void writeImpl()
	{
		writeB(new byte[] {0x0c, 0x0f});
		// writeB(new byte[149]);
		writeC(0);// походу команда которая выигрывает
		writeH(3); // что это???
		writeH(2); // что это???

		// Опыт
		for(int i = 0; i < 16; i++)
		{
			Player p = r.getPlayerBySlot(i);
			if(p != null)
			{
				int exp = p.getRoom().getSlots()[p.getSlot()].allKills * 5;
				writeH(exp);
				p.getPlayerInfos().setExp(p.getPlayerInfos().getExp() + exp);
				p.store(true, false);
			}
			else
			{
				// вслучае если нету плеера
				writeH();
			}
		}
		// Очки
		for(int i = 0; i < 16; i++)
		{
			Player p = r.getPlayerBySlot(i);
			if(p != null)
			{
				int gp = p.getRoom().getSlots()[p.getSlot()].allKills * 25;
				writeH(gp);
				p.getPlayerInfos().setGamePoint(p.getPlayerInfos().getGamePoint() + gp);
				p.store(true, false);
				// временный фикс
			}
			else
			{
				// вслучае если нету плеера
				writeH();
			}
			r.getSlot(i).gp = 0;
			r.getSlot(i).sp = 0;
			r.getSlot(i).allKills = 0;
			r.getSlot(i).oneTimeKills = 0;
			r.getSlot(i).allDeaths = 0;
			r.getSlot(i).botScore = 0; // TODO Room.clearSlot(int slot)
		}
		//Очки за ботов
		for(int i = 0; i < 16; i++)
		{
			Player p = r.getPlayerBySlot(i);
			if(p != null && r.special == 6)
			{
				SLOT slot = r.getSlot(i);
				writeH(slot.botScore);
			}
			else
			{
				writeH();
			}
		}
		
		writeB(new byte[] {
			// Опыт летит по 2 байта
			// 0x24, 0x00, 0x4c, 0x00, 0x00, 0x00,//
			// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,//
			// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,//
			// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,//
			// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,//
			// 0x00, 0x00, //
			// Очки
			// 0x16, 0x00, 0x39, 0x00, 0x00, 0x00,//
			// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,//
			// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,//
			// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,//
			// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,//
			// 0x00, 0x00, //
			// ХЗ - походу очки на ботах
			//0x00, 0x00, 0x00, 0x00, 0x00, 0x00,//
			//0x00, 0x00, 0x00, 0x00, 0x00, 0x00,//
			//0x00, 0x00, 0x00, 0x00, 0x00, 0x00,//
			//0x00, 0x00, 0x00, 0x00, 0x00, 0x00,//
			//0x00, 0x00, 0x00, 0x00, 0x00, 0x00,//
			//0x00, 0x00,//
			// тоже ХЗ
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00,//
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00,//
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00,//
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00,//
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00,//
			0x00, 0x00,//
			// и тут ХЗ
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00,//
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00,//
			0x00, 0x00, 0x00, 0x00
		//
		});

		writeS(_player.getName(), 33); // Имя перса
		writeD(_playerInfo.getExp()); // опыт
		writeD(_playerInfo.getRank()); // ранк (0-54)
		writeD(0); // Пока не понятно за чего отвечают пустые байты...
		writeD(_playerInfo.getGamePoint()); // ГП
		writeD(0); // Рублики

		writeB(new byte[13]); // Хз что это такое

		writeD(0); // лычка =)
		writeH(0);

		// КланИнфа
		writeS("Клан отсутствует", 16); // Clan name 16 символов =)
		writeC(); // что то типа разделителя между названием клана и его рангом
		writeH(1); // Ранг клана - 53 - max

		// ClanLogo logo = clan.getClanLogo();
		writeC(255); // Лого1
		writeC(255); // Лого2
		writeC(255); // Лого3
		writeC(255); // Лого4
		writeH(0); // цвет названия клана.
		writeD(); // Непонятно чо

		// ===================

		// НЕ СЕЗОН
		writeD(_playerInfo.getFights(false)); // Количество боев
		writeD(_playerInfo.getWinFights(false)); // Количество побед
		writeD(_playerInfo.getLostFights(false)); // Количество поражениев
		writeD(); // Непонятно чо
		writeD(_playerInfo.getKills(false)); // Количество убийств
		writeD(); // Непонятно чо
		writeD(_playerInfo.getDeaths(false)); // Количество смертей
		writeD(); // Непонятно чо
		writeD(); // Непонятно чо
		writeD(_playerInfo.getEscapes(false)); // Количество побегов с поля боя
		// ==========================

		// СЕЗОН
		writeD(_playerInfo.getFights(true)); // Количество боев
		writeD(_playerInfo.getWinFights(true)); // Количество побед
		writeD(_playerInfo.getLostFights(true)); // Количество поражений
		writeD(); // Непонятно чо
		writeD(_playerInfo.getKills(true)); // Количество убийств
		writeD(); // Непонятно чо
		writeD(_playerInfo.getDeaths(true)); // Количество смертей
		writeD(); // Непонятно чо
		writeD(); // Непонятно чо
		writeD(_playerInfo.getEscapes(true)); // Количество побегов с поля боя
		// =============================

		writeB(new byte[21]);

		// writeB(new byte[] {0x0c, 0x0f, 0x01, 0x03, 0x00, 0x00, 0x00, 0x24, 0x00, 0x4c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0x00, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41,
		// 0x6e, 0x61, 0x4c, 0x67, 0x69, 0x4e, 0x2a, 0x52, 0x55, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		// 0x00, 0x7f, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xfe, (byte) 0xdf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

		// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte)
		// 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6c, 0x00,
		// 0x00, 0x00, 0x31, 0x00, 0x00, 0x00, 0x4c, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x6c, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x03,
		// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6c, 0x00, 0x00, 0x00, 0x31, 0x00, 0x00, 0x00, 0x4c, 0x00, 0x00, 0x00, 0x09,
		// 0x00, 0x00, 0x00, 0x6c, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		// 0x00});

		// 0c:0f:
		// 01:03:00:00:00:24:00:4c:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:16:00:39:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:41:6e:61:4c:67:69:4e:2a:52:55:53:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:7f:04:00:00:00:00:00:00:00:00:00:00:fe:df:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:ff:ff:ff:ff:00:00:00:00:00:00:09:00:00:00:06:00:00:00:03:00:00:00:00:00:00:00:6c:00:00:00:31:00:00:00:4c:00:00:00:09:00:00:00:6c:00:00:00:02:00:00:00:09:00:00:00:06:00:00:00:03:00:00:00:00:00:00:00:6c:00:00:00:31:00:00:00:4c:00:00:00:09:00:00:00:6c:00:00:00:02:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:4f:00:08:0e:00:00:00:00:42:41:54:54:41:47:4c:49:41:20:46:45:52:4f:43:45:20:21:21:21:00:72:00:03:00:00:01:00:02:02:02:0f:20:00:41:6e:61:4c:67:69:4e:2a:52:55:53:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:32:00:00:00:00:00:01:00:64:02:02:0e:00:00:00:00:06:00:00:00:00:00:00:00:00:00:00:ff:ff:ff:ff:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:06:01:00:00:00:00:00:00:00:00:00:ff:ff:ff:ff:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:01:00:00:00:00:00:00:00:00:00:00:ff:ff:ff:ff:00:00:00:00:00:00:00:53:53:41:53:49:4e:5f:5f:44:45:4c:54:41:00:5f:00:01:00:00:00:00:00:00:00:00:00:00:ff:ff:ff:ff:00:00:00:00:00:00:00:54:5d:68:65:5f:5b:44:5d:75:6d:62:61:73:73:99:00:01:00:00:00:00:00:00:00:00:00:00:ff:ff:ff:ff:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:01:00:00:00:00:00:00:00:00:00:00:ff:ff:ff:ff:00:00:00:00:00:00:00:b0:44:49:46:46:33:52:33:4e:43:33:b0:b0:00:00:00:01:00:00:00:00:00:00:00:00:00:00:ff:ff:ff:ff:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:01:00:00:00:00:00:00:00:00:00:00:ff:ff:ff:ff:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:01:00:00:00:00:00:00:00:00:00:00:ff:ff:ff:ff:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:01:00:00:00:00:00:00:00:00:00:00:ff:ff:ff:ff:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:01:00:00:00:00:00:00:00:00:00:00:ff:ff:ff:ff:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:01:00:00:00:00:00:00:00:00:00:00:ff:ff:ff:ff:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:01:00:00:00:00:00:00:00:00:00:00:ff:ff:ff:ff:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:01:00:00:00:00:00:00:00:00:00:00:ff:ff:ff:ff:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:01:00:00:00:00:00:00:00:00:00:00:ff:ff:ff:ff:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:01:00:00:00:00:00:00:00:00:00:00:ff:ff:ff:ff:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00
	}
}
